import * as types from "./mutation-types";
import Api from "api/api";
import _ from "lodash";
const state = {
  taskList: {},
  currentTask: {},
  searchTaskCondition: {
    pageSize: 10,
    title: "",
    userType: "",
    startTime: "",
    status: "",
    departId: "",
    departName: ""
  }
};

const getters = {
  taskList: state => state.taskList,
  currentTask: state => state.currentTask,
  searchTaskCondition: state => state.searchTaskCondition
};

const actions = {
  getTaskPageS({ commit }, param) {
    return Api.getTaskPage(param).then(ret => {
      commit(types.UPDATE_TASK_LIST, ret.data.data);
      return ret;
    });
  },
  getTaskS({ commit }, id) {
    return Api.getTask({ id: id }).then(ret => {
      var payload = ret.data.data;
      commit(types.UPDATE_CURRENT_TASK, payload);
      return ret;
    });
  },
  addOrUpdateTaskS({ commit }, payload) {
    return Api.addOrUpdateTask({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteTaskS({ commit }, param) {
    return Api.deleteTask(param).then(ret => {
      return ret;
    });
  },
  updateQuests({ commit, state }, payload) {
    var tem = _.cloneDeep(state.currentTask.quests);
    payload.map((item, index) => {
      var existed = false;
      tem.map((item2, ind) => {
        if (item2.id === item.id) {
          existed = true;
        }
      });
      if (!existed) {
        tem.push(item);
      }
    });
    console.log(tem);
    commit(types.UPDATE_CURRENT_TASK_QUEST, tem);
  },
  clearQuests({ commit }) {
    var tem = [];
    commit(types.CLEAR_CURRENT_TASK_QUEST, tem);
  }
};
const mutations = {
  [types.UPDATE_TASK_LIST](state, payload) {
    state.taskList = payload;
  },
  [types.INIT_CURRENT_TASK](state, payload) {
    state.currentTask = {
      title: "",
      userType: "all",
      questType: "General",
      quests: [],
      startTime: "",
      endTime: ""
    };
  },
  [types.UPDATE_CURRENT_TASK](state, payload) {
    state.currentTask = payload;
  },
  [types.UPDATE_CURRENT_TASK_QUEST](state, payload) {
    state.currentTask.quests = payload;
  },
  [types.CLEAR_CURRENT_TASK_QUEST](state, payload) {
    state.currentTask.quests = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
